package com.wgh.inventory.controller;

import com.wgh.inventory.entity.Inventory;
import com.wgh.inventory.service.InventoryService;
import com.wgh.common.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存Controller
 * 提供库存相关的RESTful API接口
 */
@RestController
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    /**
     * 根据SKU ID获取库存信息
     */
    @GetMapping("/sku/{skuId}")
    public Result<Inventory> getInventoryBySkuId(@PathVariable Long skuId) {
        try {
            Inventory inventory = inventoryService.getInventoryBySkuId(skuId);
            if (inventory == null) {
                return Result.error("404", "库存信息不存在");
            }
            return Result.success(inventory);
        } catch (Exception e) {
            return Result.error("获取库存信息失败：" + e.getMessage());
        }
    }

    /**
     * 扣减库存
     */
    @PutMapping("/sku/{skuId}/decrease")
    public Result<Boolean> decreaseInventory(
            @PathVariable Long skuId,
            @RequestParam Integer quantity) {
        try {
            boolean result = inventoryService.decreaseInventory(skuId, quantity);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("库存不足或扣减失败");
            }
        } catch (Exception e) {
            return Result.error("扣减库存失败：" + e.getMessage());
        }
    }

    /**
     * 增加库存
     */
    @PutMapping("/sku/{skuId}/increase")
    public Result<Boolean> increaseInventory(
            @PathVariable Long skuId,
            @RequestParam Integer quantity) {
        try {
            boolean result = inventoryService.increaseInventory(skuId, quantity);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("增加库存失败");
            }
        } catch (Exception e) {
            return Result.error("增加库存失败：" + e.getMessage());
        }
    }

    /**
     * 锁定库存
     */
    @PutMapping("/sku/{skuId}/lock")
    public Result<Boolean> lockInventory(
            @PathVariable Long skuId,
            @RequestParam Integer quantity) {
        try {
            boolean result = inventoryService.lockInventory(skuId, quantity);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("库存不足或锁定失败");
            }
        } catch (Exception e) {
            return Result.error("锁定库存失败：" + e.getMessage());
        }
    }

    /**
     * 解锁库存
     */
    @PutMapping("/sku/{skuId}/unlock")
    public Result<Boolean> unlockInventory(
            @PathVariable Long skuId,
            @RequestParam Integer quantity) {
        try {
            boolean result = inventoryService.unlockInventory(skuId, quantity);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("解锁库存失败");
            }
        } catch (Exception e) {
            return Result.error("解锁库存失败：" + e.getMessage());
        }
    }

    /**
     * 检查库存是否充足
     */
    @GetMapping("/sku/{skuId}/check")
    public Result<Boolean> checkInventory(
            @PathVariable Long skuId,
            @RequestParam Integer quantity) {
        try {
            boolean result = inventoryService.checkInventory(skuId, quantity);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("检查库存失败：" + e.getMessage());
        }
    }

    /**
     * 更新库存预警值
     */
    @PutMapping("/sku/{skuId}/warning")
    public Result<Boolean> updateWarningQuantity(
            @PathVariable Long skuId,
            @RequestParam Integer warningQuantity) {
        try {
            boolean result = inventoryService.updateWarningQuantity(skuId, warningQuantity);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("更新库存预警值失败");
            }
        } catch (Exception e) {
            return Result.error("更新库存预警值失败：" + e.getMessage());
        }
    }

    /**
     * 批量扣减库存
     */
    @PutMapping("/batch/decrease")
    public Result<Boolean> batchDecreaseInventory(
            @RequestParam Long[] skuIds,
            @RequestParam Integer[] quantities) {
        try {
            if (skuIds == null || quantities == null || skuIds.length != quantities.length) {
                return Result.error("参数不合法");
            }
            boolean result = inventoryService.batchDecreaseInventory(skuIds, quantities);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("批量扣减库存失败");
            }
        } catch (Exception e) {
            return Result.error("批量扣减库存失败：" + e.getMessage());
        }
    }

    /**
     * 获取库存预警列表
     */
    @GetMapping("/warning")
    public Result<List<Inventory>> getInventoryWarningList() {
        try {
            List<Inventory> warningList = inventoryService.getInventoryWarningList();
            return Result.success(warningList);
        } catch (Exception e) {
            return Result.error("获取库存预警列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取库存信息
     */
    @GetMapping("/{id}")
    public Result<Inventory> getInventoryById(@PathVariable Long id) {
        try {
            Inventory inventory = inventoryService.getById(id);
            if (inventory == null) {
                return Result.error("404", "库存信息不存在");
            }
            return Result.success(inventory);
        } catch (Exception e) {
            return Result.error("获取库存信息失败：" + e.getMessage());
        }
    }

    /**
     * 保存库存信息
     */
    @PostMapping
    public Result<Inventory> saveInventory(@RequestBody Inventory inventory) {
        try {
            boolean result = inventoryService.save(inventory);
            if (result) {
                return Result.success(inventory);
            } else {
                return Result.error("保存库存信息失败");
            }
        } catch (Exception e) {
            return Result.error("保存库存信息失败：" + e.getMessage());
        }
    }

    /**
     * 更新库存信息
     */
    @PutMapping
    public Result<Boolean> updateInventory(@RequestBody Inventory inventory) {
        try {
            if (inventory.getId() == null) {
                return Result.error("库存ID不能为空");
            }
            boolean result = inventoryService.updateById(inventory);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("更新库存信息失败");
            }
        } catch (Exception e) {
            return Result.error("更新库存信息失败：" + e.getMessage());
        }
    }
}